package FourArithmeticOperations;

/**
 * Created by dell on 2017/10/24.
 */

public class LinkedBinarySearchTree<T extends Comparable<T>> extends LinkedBinaryTree<T> implements BinarySearchTree<T>
{

    public LinkedBinarySearchTree()
    {
        super();
    }

    public LinkedBinarySearchTree (T element)
    {
        root = new BSTNode<T>(element);
    }

    public void add (T item)
    {
        if (root == null)
            root = new BSTNode<T>(item);
        else
            ((BSTNode)root).add(item);
    }

    public T remove (T target)
    {
        BSTNode<T> node = null;

        if (root != null)
            node = ((BSTNode)root).find(target);

        root = ((BSTNode)root).remove(target);

        return node.getElement();
    }

     public T findMin() {
        BTNode<T> node = root;
        if (node.left==null)
            return node.getElement();
        else
            while (node.getLeft()!=null){
                node=node.getLeft();
            }
       return node.getElement();
     }
     public T findMax() {
         BTNode<T> node = root;
        if (node.right==null)
         return node.getElement();
     else
         while (node.getRight()!=null){
             node=node.getRight();
            }
         return node.getElement();
     }
}